package com.springboot_gradrecruit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.mapper.ImageMapper;
import com.springboot_gradrecruit.mapper.ReportMapper;
import com.springboot_gradrecruit.model.dto.report.AddReportRequest;
import com.springboot_gradrecruit.model.dto.report.PageReportRequest;
import com.springboot_gradrecruit.model.dto.report.UpdateReportRequest;
import com.springboot_gradrecruit.model.entity.Image;
import com.springboot_gradrecruit.model.entity.Report;
import com.springboot_gradrecruit.model.entity.User;
import com.springboot_gradrecruit.model.vo.report.ReportVO;
import com.springboot_gradrecruit.service.ReportService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author zr
* @description 针对表【report】的数据库操作Service实现
* @createDate 2024-12-13 10:18:29
*/
@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report>
    implements ReportService {

    @Resource
    private ReportMapper reportMapper;

    @Resource
    private ImageMapper imageMapper;


    @Override
    public Long addReport(AddReportRequest addReportRequest) {
        //1.获取参数
        Report report = new Report();
        report.setReportUserId(addReportRequest.getReportUserId());
        report.setReportUserRole(addReportRequest.getReportUserRole());
        report.setReportPersonName(addReportRequest.getReportPersonName());
        report.setReportPersonAccount(addReportRequest.getReportPersonAccount());
        report.setReportPersonContact(addReportRequest.getReportPersonContact());
        report.setReportDescribe(addReportRequest.getReportDescribe());
        report.setReportStatus(addReportRequest.getReportStatus());
        report.setReportCheckStatus(addReportRequest.getReportCheckStatus());
        report.setReportCheckRemark(addReportRequest.getReportCheckRemark());

        //2.插入数据
        int insert = reportMapper.insert(report);

        //3.获取图片列表
        List<String> imageList = addReportRequest.getImageList();
        System.out.println(imageList);
        Long reportId = report.getReportId();
        //插入图片
        for (String image : imageList) {
            Image image1 = new Image();
            image1.setImageName("report");
            image1.setImageNameId(reportId);
            image1.setImageUrl(image);
            imageMapper.insert(image1);
        }
        return (long) insert;

    }

    @Override
    public boolean updateReport(UpdateReportRequest updateReportRequest) {

        //1.获取参数
        Long reportId = updateReportRequest.getReportId();
        String reportPersonContact = updateReportRequest.getReportPersonContact();
        String reportDescribe = updateReportRequest.getReportDescribe();
        String reportStatus = updateReportRequest.getReportStatus();
        String reportCheckStatus = updateReportRequest.getReportCheckStatus();
        String reportCheckRemark = updateReportRequest.getReportCheckRemark();
        List<String> imageList = updateReportRequest.getImageList();

        //2.更新数据-反馈举报
        LambdaUpdateWrapper<Report> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Report::getReportId, reportId)
                .set(StrUtil.isNotBlank(reportPersonContact), Report::getReportPersonContact, reportPersonContact)
                .set(StrUtil.isNotBlank(reportDescribe), Report::getReportDescribe, reportDescribe)
                .set(StrUtil.isNotBlank(reportStatus), Report::getReportStatus, reportStatus)
                .set(StrUtil.isNotBlank(reportCheckStatus), Report::getReportCheckStatus, reportCheckStatus)
                .set(StrUtil.isNotBlank(reportCheckRemark), Report::getReportCheckRemark, reportCheckRemark);
        int update = reportMapper.update(null, updateWrapper);

        if(imageList!=null){
            //3.更新图片
            imageMapper.delete(new LambdaUpdateWrapper<Image>().eq(Image::getImageNameId, reportId));//删除原来的图片
            for (String image : imageList) {
                Image image1 = new Image();
                image1.setImageName("report");
                image1.setImageNameId(reportId);
                image1.setImageUrl(image);
                imageMapper.insert(image1);
            }
        }
        return update > 0;
    }

    @Override
    public PageResponse<ReportVO> pageReport(PageReportRequest pageReportRequest) {

        //1.获取参数
        String reportStatus = pageReportRequest.getReportStatus();
        String reportCheckStatus = pageReportRequest.getReportCheckStatus();

        //2.构建分页条件
        Page<Report> page = pageReportRequest.toPageDefaultSortByUpdateTime();

        //3.分页查询
        Page<Report> reportPage = lambdaQuery()
                .eq(StrUtil.isNotBlank(reportStatus), Report::getReportStatus, reportStatus)
                .eq(StrUtil.isNotBlank(reportCheckStatus), Report::getReportCheckStatus, reportCheckStatus)
                .page(page);

        return PageResponse.of(reportPage, ReportVO.class);

    }
}




